package com.example.demo.java8;

import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author f_bao
 * @create 2018/8/7
 */
public class RegexUtils {

    public static String replace(String content, Pattern regex, Function<Matcher, String> func) {
        StringBuffer sb = new StringBuffer();
        Matcher matcher = regex.matcher(content);

        while (matcher.find()) {
            String apply = func.apply(matcher);
            matcher.appendReplacement(sb, apply);
        }

        return sb.toString();
    }
}
